/**
 * Copyright (C) 2010-2013 Alibaba Group Holding Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.alibaba.rocketmq.example.quickstart;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.alibaba.rocketmq.client.producer.DefaultMQProducer;
import com.alibaba.rocketmq.client.producer.SendResult;
import com.alibaba.rocketmq.common.message.Message;

/**
 * Producer，发送消息
 * 
 */
public class ProducerSpring {
	@SuppressWarnings("resource")
	public static void main(String[] args) throws Exception {
		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("mq-producer-bean.xml");
		DefaultMQProducer producerMarket = (DefaultMQProducer) ctx.getBean("producerMarket");
		producerMarket.start();

		try {
			Message msg = new Message("TopicTest", // topic
					"TagA", // tag
					("Hello RocketMQ1 ").getBytes()// body
			);
			SendResult sendResult = producerMarket.send(msg);
			System.out.println(sendResult);
		} catch (Exception e) {
			e.printStackTrace();
			Thread.sleep(1000);
		}

		producerMarket.shutdown();

		DefaultMQProducer producerBalance = (DefaultMQProducer) ctx.getBean("producerBalance");
		producerBalance.start();

		try {
			Message msg = new Message("TopicTest", // topic
					"TagB", // tag
					("Hello RocketMQ2 ").getBytes()// body
			);
			SendResult sendResult = producerBalance.send(msg);
			System.out.println(sendResult);
		} catch (Exception e) {
			e.printStackTrace();
			Thread.sleep(1000);
		}

		producerBalance.shutdown();

		DefaultMQProducer producerOperate = (DefaultMQProducer) ctx.getBean("producerOperate");
		producerOperate.start();

		try {
			Message msg = new Message("TopicTest", // topic
					"TagC", // tag
					("Hello RocketMQ3 ").getBytes()// body
			);
			SendResult sendResult = producerOperate.send(msg);
			System.out.println(sendResult);
		} catch (Exception e) {
			e.printStackTrace();
			Thread.sleep(1000);
		}

		producerOperate.shutdown();

	}
}
